package com.example.demo.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class JsonAndBean {
    private ObjectMapper mapper=new ObjectMapper();
    public <T> T JsonToBean(String json,Class<T> tClass){
        T t = null;
        try {
            t = mapper.readValue(json, tClass);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return t;
    }

    public String BeanToJson(Object object){
        String jsonValue = null;
        try {
            jsonValue = mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonValue;
    }
}
